unit UToolsList;

interface
uses sysutils, Contnrs, inifiles;
type
  TToolsProperty = class
  public
    Title: string;
    ProgramName: string;
    WorkDir: string;
    Parameter: string;
  end;
  TToolsList = class(TobjectList)
  private
    function GetItems(Index: Integer): TToolsProperty;
    procedure SetItems(Index: Integer; AToolsProperty: TToolsProperty);
    procedure HandleFreeNotify(Sender: TObject; AToolsProperty: TToolsProperty);
  protected

  public
    destructor Destroy; override;

    function Add(AToolsProperty: TToolsProperty): Integer;
    function Extract(Item: TToolsProperty): TToolsProperty;
    function Remove(AToolsProperty: TToolsProperty): Integer;
    function IndexOf(AToolsProperty: TToolsProperty): Integer;
    function First: TToolsProperty;
    function Last: TToolsProperty;
    procedure LoadFromFile(FileName: string);
    procedure SaveToFile(FileName: string);
    procedure Insert(Index: Integer; AToolsProperty: TToolsProperty);
    property Items[Index: Integer]: TToolsProperty read GetItems write SetItems; default;
  end;
var
  aToolsList: TToolsList;
implementation

{ TToolsList }

function TToolsList.Add(AToolsProperty: TToolsProperty): Integer;
begin
  Result := inherited Add(AToolsProperty);
end;

destructor TToolsList.Destroy;
var
  i: integer;
begin
  for i := count - 1 downto 0 do
    remove(items[i]);
  inherited;
end;

function TToolsList.Extract(Item: TToolsProperty): TToolsProperty;
begin
  Result := TToolsProperty(inherited Extract(Item));
end;

function TToolsList.First: TToolsProperty;
begin
  Result := TToolsProperty(inherited First);
end;

function TToolsList.GetItems(Index: Integer): TToolsProperty;
begin
  Result := TToolsProperty(inherited Items[Index]);
end;

procedure TToolsList.HandleFreeNotify(Sender: TObject;
  AToolsProperty: TToolsProperty);
begin
  Extract(AToolsProperty);
end;

function TToolsList.IndexOf(AToolsProperty: TToolsProperty): Integer;
begin
  Result := inherited IndexOf(AToolsProperty);
end;

procedure TToolsList.Insert(Index: Integer;
  AToolsProperty: TToolsProperty);
begin
  inherited Insert(Index, AToolsProperty);
end;

function TToolsList.Last: TToolsProperty;
begin
  Result := TToolsProperty(inherited Last);
end;

procedure TToolsList.LoadFromFile(FileName: string);
var
  Ini: TIniFile;
  i: Integer;
  aToolsProperty: TToolsProperty;
begin
  Ini := TIniFile.Create(FileName);
  for i := 1 to Ini.ReadInteger('Count', 'Count', 0) do
  begin
    aToolsProperty := TToolsProperty.Create;
    aToolsProperty.Title := ini.ReadString(IntToStr(i), 'Title', '');
    aToolsProperty.ProgramName := ini.ReadString(IntToStr(i), 'ProgramName', '');
    aToolsProperty.WorkDir := ini.ReadString(IntToStr(i), 'WorkDir', '');
    aToolsProperty.Parameter := ini.ReadString(IntToStr(i), 'Parameter', '');
    Add(aToolsProperty);
  end;
  Ini.Free;
end;

function TToolsList.Remove(AToolsProperty: TToolsProperty): Integer;
begin
  Result := inherited Remove(AToolsProperty);
end;

procedure TToolsList.SaveToFile(FileName: string);
var
  i: Integer;
  Ini: TIniFile;
begin
  Ini := TIniFile.Create(FileName);
  ini.WriteInteger('Count', 'Count', Count);
  for i := 1 to Count do
  begin
    Ini.WriteString(IntToStr(i), 'Title', Items[i - 1].Title);
    Ini.WriteString(IntToStr(i), 'ProgramName', Items[i - 1].ProgramName);
    Ini.WriteString(IntToStr(i), 'WorkDir', Items[i - 1].WorkDir);
    Ini.WriteString(IntToStr(i), 'Parameter', Items[i - 1].Parameter);
  end;
  Ini.Free;
end;

procedure TToolsList.SetItems(Index: Integer;
  AToolsProperty: TToolsProperty);
begin
  inherited Items[Index] := AToolsProperty;
end;

end.

